﻿#include "../../ctlHelp.h"
#include <limits>
#include <cstring>

namespace swui
{
	namespace ctlListSortFunc
	{
		inline long _watol(std::wstring::const_pointer n)
		{
			if(*n == 0) 
				return std::numeric_limits<long>::min(); 
			long sign = 1;
			if(*n == L'-')
			{
				++n;
				sign = -1;
			} else if(*n == L'+') ++n;
			long result = 0;
			while(*n >= 0x30 && *n <= 0x39)
			{
				result *= 10;
				result += *n++ - 0x30;
			}
			return result;
		}
		bool charAscending(
			std::wstring::const_pointer a,
			std::wstring::const_pointer b)
		{
			return std::wcscmp(a, b) < 0;
		}
		bool longAscending(
			std::wstring::const_pointer a,
			std::wstring::const_pointer b)
		{
			return _watol(a) < _watol(b);
		}
		inline unsigned long _watoul2(std::wstring::const_pointer  n)
		{
			if(*n == 0) 
				return std::numeric_limits<unsigned long>::min(); 
			unsigned long result = 0;
			while((*n >> 1) == 0x18)
			{
				result <<= 1;
				result |= *n++ & 1;
			}
			return result;
		}
		inline long _watol2(std::wstring::const_pointer n)
		{
			if(*n == 0) 
				return std::numeric_limits<long>::min(); 
			long sign = 1;
			if(*n == L'-')
			{
				++n;
				sign = -1;
			} else if(*n == L'+') ++n;
			if(*n == 0) return sign;
			return static_cast<long>(_watoul2(n)) * sign;
		}
		bool binAscending(
			std::wstring::const_pointer a,
			std::wstring::const_pointer b)
		{
			return _watol2(a) < _watol2(b);
		}
		bool ubinAscending(
			std::wstring::const_pointer a,
			std::wstring::const_pointer b)
		{
			return _watoul2(a) < _watoul2(b);
		}
	}
}